﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;

public partial class admin_ProductCategoryDetail : SitePage
{
    #region Fields
    public string Command
    {

        get
        {
            object o = ViewState["Command"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["Command"] = value; }

    }
    #endregion

    #region Init,Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            LoadData();
    }
    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        InitData();
    }
    #endregion

    #region Methods
    private void LoadData()
    {
        if (this.Command == "edit")
        {
            int cateID = clsQueryString.GetInt("id");
            busCategory objBus = new busCategory();
            Category objCate = objBus.GetByID(cateID);
            if (objCate != null)
            {
                txtCategoryName.Text = objCate.Name;
                txtDescription.Text = objCate.Description;
                txtPageTitle.Text = objCate.PageTitle;
                txtMetaKeywords.Text = objCate.MetaKeywords;
                txtMetaDescription.Text = objCate.MetaDescription;
                if (objCate.IsActive)
                    chkIsActive.Checked = true;
                else chkIsActive.Checked = false;
                drlParent.SelectedValue = objCate.ParentId.ToString();
            }
        }
    }
    private void InitData()
    {
        string validateGroup = "cateDetail";
        vsumCategory.ValidationGroup = btnSave.ValidationGroup = reqCategoryName.ValidationGroup = validateGroup;
        //Init message
        reqCategoryName.ErrorMessage = string.Format(Resources.Resource.MSG_REQUIREMENT, "Category name");
        this.Command = clsQueryString.GetString("act");
        LoadParent();
    }

    private void UpdateCategory(Category objCate)
    {
        busCategory objBus = new busCategory();
        objCate.CategoryID = clsQueryString.GetInt("id");
        if (objBus.Update(objCate))
            Response.Redirect("ProductCategoryList.aspx");
        else lblErrorMessage.Text = Resources.Resource.MSG_UPDATE_ERROR;
    }

    private void InsertCategory(Category objCate)
    {
        busCategory objBus = new busCategory();
        if (objBus.Insert(objCate))
            Response.Redirect("ProductCategoryList.aspx");
        else lblErrorMessage.Text = Resources.Resource.MSG_INSERT_ERROR;
    }
    private void LoadParent()
    {
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.Condition = " ParentId is null and Type=" + (byte)enmCategoryType.Product;
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        busCategory objBus = new busCategory();
        CategoryCollection lstLevel1 = objBus.List(objCondition);//only return one object
        bool addEmpty = true;
        if (lstLevel1 != null && lstLevel1.Count > 0)
        {
            addEmpty = false;
            foreach (Category objData in lstLevel1)
            {
                AddNode(objData);
            }

        }
        if (addEmpty)

            drlParent.Items.Insert(0, new ListItem("- - -", string.Empty));
    }
    private void AddNode(Category objData)
    {
        string prefix = string.Empty;
        for (int i = 2; i <= objData.Level; i++)
        {
            prefix += "--";
        }
        drlParent.Items.Add(new ListItem(prefix + objData.Name, objData.CategoryID.ToString()));
        CategoryCollection lstChild = GetChild(objData);
        if (lstChild != null && lstChild.Count > 0)
        {
            foreach (Category objChild in lstChild)
            {
                AddNode(objChild);
            }
        }
    }
    private CategoryCollection GetChild(Category objData)
    {
        Category objCondition = new Category();
        objCondition.OrderBy = busCategory.SORTORDER;
        objCondition.OrderDirection = "ASC";
        objCondition.Condition = " ParentId=" + objData.CategoryID + " and Type=" + (byte)enmCategoryType.Product;
        objCondition.CurrentPage = 1;
        objCondition.PageSize = int.MaxValue;
        busCategory objBus = new busCategory();
        CategoryCollection lstResult = objBus.List(objCondition);
        return lstResult;
    }
    #endregion

    #region Events

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            //get datavalue
            Category objCate = new Category();
            objCate.ParentId = clsConvert.Int(drlParent.SelectedValue);
            objCate.Type = (byte)enmCategoryType.Product;
            objCate.CreatedBy = this.userloginName;
            objCate.CreatedDate = clsDateTime.Now;
            objCate.Description = txtDescription.Text;
            if (chkIsActive.Checked)
                objCate.IsActive = true;
            else
                objCate.IsActive = false;
            objCate.UpdatedBy = this.userloginName;
            objCate.UpdatedDate = clsDateTime.Now;
            objCate.Name = txtCategoryName.Text;
            objCate.PageTitle = txtPageTitle.Text;
            objCate.MetaKeywords = txtMetaKeywords.Text;
            objCate.MetaDescription = txtMetaDescription.Text;
            if (this.Command == "edit")
                UpdateCategory(objCate);
            else
                InsertCategory(objCate);
        }
        else
        {
            vsumCategory.ShowMessageBox = false;
            vsumCategory.ShowSummary = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductCategoryList.aspx");
    }
    #endregion
}